package com.fanqj.resilience4j.spring.service.impl;

import com.fanqj.resilience4j.spring.service.ITestService;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author <a href="mailto:fanqijin@wxchina.com">fanqj</a>
 * @Date 2021年01月27日 16:36
 * @Version 6.4.1
 */
@Service("TestServiceWithSpringImpl")
public class TestServiceWithSpringImpl implements ITestService {
    public static final Logger logger = LoggerFactory.getLogger(TestServiceWithSpringImpl.class);
    private volatile  int i = 0;

    @Override
    @CircuitBreaker(name = "backend1", fallbackMethod = "circuitBreakerFallBack")
    public String test(String name) {
        i++;
        if (i % 2 == 0) {
            throw new RuntimeException("test method having an error");
        }
        return "Hello " + name;
    }

    private String circuitBreakerFallBack(String name, RuntimeException exception) {
        logger.error("Having an error, enter fallBack method.");
        return "CircuitBreaker with fallBack";
    }

    @Override
    public String testBulkhead(String name) {
        return null;
    }

    @Override
    public String testRateLimit(String name) {
        return null;
    }

    @Override
    public String testTimeLimit(long time) {
        return null;
    }


}
